Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • scatter Y X || lfit Y X ||, by(variable)

    Good evening,

    I'd like to run scatter Y X || lfit Y X ||, by(variable)
    but have everything on one graph instead of multiple graphs. I realize that there's going to be a lot of points and lines superimposed; that's OK. Is there a way to do it?

    Thank you,
    Stan

  • #2
    Here is some technique.

    Code:
    sysuse auto, clear
    
    separate mpg, by(rep78) veryshortlabel 
    
    local call 
    forval j = 1/5 {
        local col : word `j' of red blue black magenta teal 
        local sym : word `j' of Oh Dh Sh Th X 
        local call `call' scatter mpg`j' weight, mc(`col') ms(`sym') || lfit mpg`j' weight, lw(medthick) lc(`col') || 
    }
    
    `call', ytitle(Miles per gallon) subtitle(Linear fits by Repair record 1978) legend(order(1 3 5 7 9) col(1) ring(0) pos(1))
    The use of separate beforehand saves specifying if qualifiers on every graph call.

    If your group variable does not run 1 up then egen, group() will easily get a variable of the kind.

    5 groups can be messy enough, and 10, 20, 50 would evidently be messier still, as you know, and you'll need to give up at some point showing a legend or using different colours, and so forth.

    Comment


    • #3
      Perhaps you can also look into mscatter (ssc)
      one of the options was using lfit along with Over() for multiple groups

      Comment


      • #4
        Perhaps you can also look into mscatter (ssc)
        one of the options was using lfit along with Over() for multiple groups

        Comment


        • #5
          Thank you very much.
          Is there a way to express the y-axis scale as percentage rather than as number?

          Comment


          • #6
            The distinction is yours, not that of any of these commands. Otherwise put, calculate whatever percents you want before doing this.

            Or -- on another interpretation of the question -- you can present axis labels on a different scale using mylabels as written up in https://journals.sagepub.com/doi/ful...6867X221141058
            Last edited by Nick Cox; 30 Jan 2023, 02:58.

            Comment


            • #7
              Thank you; I tried the mylabels suggestion, but I think I'm placing the code in the wrong place (although I've tried a few places). Where should
              nicelabels ERpercent, local(ylabels)
              mylabels `ylabels', suffix(%) local(ylabels)

              go in the code you provided in the original reply? (ERpercent is the y-coordinate, analogous to mpg above.)

              Comment


              • #8
                Looks good so far, but your graph command then needs

                Code:
                yla(`ylabels')
                to use the axis labels so defined.

                Comment


                • #9
                  Got it.
                  Thank you very much.

                  Comment

                  Working...
                  X